package net.onirik.mao.sampler.midi;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiEvent;
import javax.sound.midi.Sequence;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;

public class SequenceGeneration {

	public static Sequence GenSeq() throws InvalidMidiDataException{
	//Partitur {{Tonhoehe, DauerInViertelNoten, AnzahlWdh},...}
	     final int DATA[][] = {
	       {0,  1, 1}, //C
	       {0,  1, 1}, //D
	       {0,  1, 1}, //E
	       {0,  1, 1}, //F
	       {0,  1, 1}, //G,G
	       {0,  1, 1}, //A,A,A,A
	       {0,  1, 1}, //G
	       {0,  1, 1}, //A,A,A,A
	       {0,  1, 1}, //G
	       {0,  1, 1}, //F,F,F,F
	       {0,  1, 1}, //E,E
	       {0,  1, 1}, //D,D,D,D
	       {0,  1, 1}  //C
	     };
	     //Sequence bauen
	     final int PPQS = 16; 
	     final int STAKKATO = 4;
	     Sequence seq = new Sequence(Sequence.PPQ, PPQS);
	     Track track = seq.createTrack();
	     long currentTick = 0;
	     ShortMessage msg;
	     //Kanal 0 auf "EnsembleStrings" umschalten
	     msg = new ShortMessage(); 
	     //msg.setMessage(ShortMessage.PROGRAM_CHANGE, 0, 0, 0);
	     track.add(new MidiEvent(msg, currentTick));
	     //Partiturdaten hinzufügen
	     for (int i = 0; i < DATA.length; ++i) { 
	       for (int j = 0; j < DATA[i][2]; ++j) { //Anzahl Wdh. je Note
	         msg = new ShortMessage();
	         msg.setMessage(ShortMessage.NOTE_ON, 0, DATA[i][0], 64);
	         track.add(new MidiEvent(msg, currentTick));
	         currentTick += PPQS * DATA[i][1] - STAKKATO;
	         msg = new ShortMessage();
	         msg.setMessage(ShortMessage.NOTE_OFF, 0, DATA[i][0], 0);
	         track.add(new MidiEvent(msg, currentTick));
	         currentTick += STAKKATO; 
	       }
	     }
	return seq;
	}
}
